var preliminary = {
	dataObj: {
		currentPage: 1, //当前页 默认为1
		numPerPage: 20, //总页数判断 每页多少条数据 默认为20
		Ob: 1, // 钢铁的种类 ：1， 废家电种类：2， 纸种类： 7
		allPage: 4 //不确定
	},
	//钢铁
	postData_steel: {
		type: 1, //种类 1，废钢铁7，废纸默认 1 废钢铁
		localcity: "", //城市
		tradeName: "", //品种
		steelMill: '', //钢厂
		currentPage: 1, //当前页 默认为1
		numPerPage: 20, //总页数判断 每页多少条数据 默认为20
		store: '' //仓库
	},
	//家电
	postData_jd: {
		type: 2, //种类 1，废钢铁7，废纸默认 1 废钢铁
		localcity: "", //城市
		specise: "", //品种
		size: '', //规格
		disassembleFactory: '', //拆解厂
		currentPage: 1, //当前页 默认为1
		numPerPage: 20 //总页数判断 每页多少条数据 默认为20
	},
	//纸
	postData_paper: {
		type: 7, //种类 1，废钢铁7，废纸默认 1 废钢铁
		localcity: "", //城市
		specise: "", //品种
		size: '', //规格
		disassembleFactory: '', //拆解厂
		currentPage: 1, //当前页 默认为1
		numPerPage: 20 //总页数判断 每页多少条数据 默认为20
	},
	search_el_steel: '<span>所在地</span><select class="whole-country"><option value="">请选择</option>' +
		'</select><span>品名</span><select class="Varieties"><option value="">请选择</option></select>' +
		'<span>材质</span><input type="text" class="cz" /><span>需求方</span><input type="text" class="gc" />' +
		'<span>仓库</span><input type="text" class="ck" /><button>找货</button>',

	search_el_home_applicance: '<select class="whole-country"><option value="">全国</option></select>' +
		'<span>品名</span><select class="household-electrical-appliances"><option value="">请选择</option>' +
		'</select><span>规格</span><select class="gui"><option value="">请选择</option></select><span>拆解厂</span>' +
		'<input type="text" class="cj" /><button>找货</button>',

	search_el_paper: '<span>所在地</span><select class="whole-country"><option value="">全国</option></select><span>' +
		'品名</span><select class="Varieties"><option value="">请选择</option></select><span>材质</span>' +
		'<input type="text" class="cz" /><span>需求方</span><input type="text" class="gc" /><span>仓库' +
		'</span><input type="text" class="ck" /><button>找货</button>',

	datatable_title_el_steel: '<div class="list-title"><div>序号</div><div>采购编码</div><div>需求方</div>' +
		'<div>所在地（城市）</div><div>类别</div><div>品名</div><div>规格描述</div><div>预计采购单价/元</div>' +
		'<div>预采量/吨</div><div>状态</div></div>',

	datatable_title_el_paper: '<div class="list-title"><div>序号</div><div>采购编码</div><div>需求方</div>' +
		'<div>所在地（城市）</div><div>类别</div><div>品名</div><div>规格描述</div><div>预计采购单价/元</div>' +
		'<div>预采量/吨</div><div>状态</div></div>',

	datatable_title_el_home_applicance: '<div class="list-title1"><div>序号</div><div>采购编码</div>' +
		'<div>拆解企业名称</div><div>所在地（城市）</div><div>品名</div><div>规格</div><div>单价</div>' +
		'<div>状态</div><div>操作</div></div>',

	datatable_loading_data_pic: '<div class="list-title-box">' +
		'<img src="images/loading.gif" class="list-title-img"></div>',
	//钢铁城市
	selected_el_city_steel: '<option value="TIANJIN">天津市</option><option value="BEIJING">北京市' +
		'</option><option value="SHANGHAI">上海市</option><option value="GUANGZHOU">广州市</option>' +
		'<option value="SHENZHEN">深圳市</option><option value="SHIJIAZHUANG">石家庄</option>',
	//钢铁品名
	selected_el_varieties_steel: '<option value="重型废钢">重型废钢</option><option value="统料废钢">' +
		'统料废钢</option><option value="中型废钢">中型废钢</option><option value="薄型废钢">薄型废钢</option>' +
		'<option value="小型废钢">小型废钢</option>',
	//家电品名
	selected_el_varieties_home_applicance: '<option value="废旧洗衣机">废旧洗衣机</option>' +
		'<option value="废旧电冰箱">废旧电冰箱</option><option value="废旧空调器">废旧空调器</option>' +
		'<option value="废旧计算机">废旧计算机</option><option value="废旧电冰柜">废旧电冰柜</option>' +
		'<option value="废旧电视机">废旧电视机</option>',
	//纸品名
	selected_el_varieties_paper: '<option value="重型废钢">箱板A级</option><option value="统料废钢">' +
		'箱板B级</option><option value="中型废钢">箱板C级</option><option value="薄型废钢">箱板统货' +
		'</option><option value="小型废钢">大花统货</option><option value="小型废钢">带皮书切页子</option>' +
		'<option value="小型废钢">混合白纸</option><option value="小型废钢">B级页子纸</option>',
	//家电的型号获取
	size_home_applicance: function(Varieties) {
		$.ajax({
			type: "post",
			url: "http://47.93.102.34:8088/cmscm/webshop/showSizeAp",
			async: true,
			data: {
				"species": Varieties
			},
			success: function(ags) {
				var Data = ags[0].content;
				$('.gui').html("");
				$('.gui').append('<option value="">请选择</option>');
				for(var i = 0; i < Data.length; i++) {
					$('.gui').append('<option value="' + Data[i] + '">' + Data[i] + '</option>');
				};
			}
		});
	},
	//家电页面品种下拉框选择 获取型号
	home_application_select_change: function() {
		$(document).on('change', '.household-electrical-appliances', function() {
			var Varieties = $(this).val();
			preliminary.size_home_applicance(Varieties);
		});
	},
	//找货
	clickSearchButton: function() {
		$(document).on('click', '.waste-search button', function() {
			preliminary.dataObj.currentPage = 1;
			var city = $('.whole-country').val();
			var pinming, cz, gc, ck;
			if(preliminary.dataObj.Ob == 1) {
				//材质
				cz = $('.cz').val();
				//需求方
				gc = $('.gc').val();
				//仓库
				ck = $('.ck').val();
				pinming = $('.Varieties').val(); //品名
				preliminary.postData_steel = {
					type: 1, //种类 1，废钢铁7，废纸默认 1 废钢铁
					localcity: city, //城市
					tradeName: pinming, //品种
					steelMill: gc, //钢厂
					currentPage: 1, //当前页 默认为1
					numPerPage: 20, //总页数判断 每页多少条数据 默认为20
					store: ck //仓库
				};

			} else if(preliminary.dataObj.Ob == 2) {
				pinming = $('.household-electrical-appliances').val(); //品名
				var size = $('.gui').val(); //家电规格
				var disfactory = $('.cj').val(); //家电拆解厂
				preliminary.postData_jd = {
					type: 2, //种类 1，废钢铁7，废纸默认 1 废钢铁
					localcity: city, //城市
					specise: pinming, //品种
					size: size, //规格
					disassembleFactory: disfactory, //拆解厂
					currentPage: 1, //当前页 默认为1
					numPerPage: 20 //总页数判断 每页多少条数据 默认为20
				};

			} else if(preliminary.dataObj.Ob == 7) {
				cz = $('.cz').val();
				gc = $('.gc').val();
				ck = $('.ck').val();
				pinming = $('.Varieties').val(); //品名
				preliminary.postData_paper = {
					type: 7, //种类 1，废钢铁7，废纸默认 1 废钢铁
					localcity: city, //城市
					specise: pinming, //品种
					size: cz, //规格
					disassembleFactory: gc, //拆解厂
					currentPage: 1, //当前页 默认为1
					numPerPage: 20 //总页数判断 每页多少条数据 默认为20
				};
			} else {
				alert('哪里出粗了')
			};
			preliminary.AJAX();
		});
	},
	//上一页 下一页是否可以点击判断
	btn_judge: function() {
		if(preliminary.dataObj.currentPage == 1) {
			$(".prePage").prop("disabled", true)
		} else {
			$(".prePage").prop("disabled", false)
		};
		if(preliminary.dataObj.currentPage == preliminary.dataObj.allPage) {
			$(".nextPage").prop("disabled", true)
		} else {
			$(".nextPage").prop("disabled", false)
		};
	},

	//根据数据的共有条数 每页20条 算出共有多少页 调用生成页数方法生成分页按钮
	numpag: function(data, currentPage) {
		$('.hang').html('');
		//共有多少页判断
		preliminary.dataObj.allPage = data[0].count / 20 < 1 ? 1 : Math.ceil(data[0].count / 20);
		//生成分页按钮
		preliminary.productPage(preliminary.dataObj.allPage, currentPage);
		//上一页 下一页是否可以点击判断
		preliminary.btn_judge();
	},

	//生成分页按钮
	productPage: function(allPage, currentPage) {
		if(allPage < 7) {
			for(var i = 1; i <= allPage; i++) {
				if(i == currentPage) {
					$('.hang').append($('<button class="num_button current">' + i + '</button>'))
				} else {
					$('.hang').append($('<button class="num_button">' + i + '</button>'))
				}
			}
		} else {
			if(currentPage <= 7) {
				for(var i = 1; i <= 7; i++) {
					if(i == currentPage) {
						$('.hang').append($('<button class="num_button current">' + i + '</button>'))
					} else {
						$('.hang').append($('<button class="num_button">' + i + '</button>'))
					}
				};
				$('.hang').append($('<span>...</span>'));
			} else {
				if(currentPage == allPage) {
					for(var i = currentPage - 6; i <= currentPage; i++) {
						if(i == currentPage) {
							$('.hang').append($('<button class="num_button current">' + i + '</button>'))
						} else {
							$('.hang').append($('<button class="num_button">' + i + '</button>'))
						}
					};
					$('.hang').prepend($('<span> ... </span>'));
				} else {
					for(var i = currentPage - 6; i <= currentPage; i++) {
						if(i == currentPage) {
							$('.hang').append($('<button class="num_button current">' + i + '</button>'))
						} else {
							$('.hang').append($('<button class="num_button">' + i + '</button>'))
						}
					};
					$('.hang').prepend($('<span> ... </span>'));
					$('.hang').append($('<span>...</span>'));
				};
			};
		};
	},
	//初始化页面
	init: function() {
		var city, pinming, size, steelMill;
		//如果url 有查询部分 则获取查询部分，
		if(location.search) {
			preliminary.dataObj.Ob = decodeURIComponent(location.search.slice(1).split('&')[4].split('=')[1]);
			city = decodeURIComponent(location.search.slice(1).split('&')[0].split('=')[1]);
			pinming = decodeURIComponent(location.search.slice(1).split('&')[1].split('=')[1]);
			size = decodeURIComponent(location.search.slice(1).split('&')[2].split('=')[1]);
			steelMill = decodeURIComponent(location.search.slice(1).split('&')[3].split('=')[1]);
			if(preliminary.dataObj.Ob == 1) {
				preliminary.postData_steel.type = 1;
				preliminary.postData_steel.localcity = city;
				preliminary.postData_steel.tradeName = pinming;
				preliminary.postData_steel.steelMill = steelMill;

			} else if(preliminary.dataObj.Ob == 2) {

				preliminary.postData_jd.type = 2;
				preliminary.postData_jd.localcity = city;
				preliminary.postData_jd.specise = pinming;
				preliminary.postData_jd.size = size;
				preliminary.postData_jd.disassembleFactory = steelMill;

			} else if(preliminary.dataObj.Ob == 7) {

				preliminary.postData_paper.type = 7;
				preliminary.postData_paper.localcity = city;
				preliminary.postData_paper.specise = pinming;
				preliminary.postData_paper.size = size;
				preliminary.postData_paper.disassembleFactory = steelMill;

			} else {
				alert('哪里出错了')
			};
			//根据查询参数 获取数据
			preliminary.AJAX();
		} else {
			//没有查询部分 直接调用AJAX方法 渲染页面
			preliminary.AJAX();
		};
	},

	//初始加载页面的数据 该方法用到的地方有 1.点击导航栏 2.从首页选择条件点击搜索 不同进入方式，传参不一样
	AJAX: function() {
		var postData;
		var currentP = preliminary.dataObj.currentPage;
		//判断表格头部菜单
		if(preliminary.dataObj.Ob == 1) {
			//Ob == 1钢铁 
			//tab按钮加高亮下划线
			$('.waste-title li').eq(0).addClass('bottom_border_red').siblings('li').removeClass('bottom_border_red');
			//搜索框渲染内容
			$('.waste-search').html('').append(preliminary.search_el_steel);
			//搜索框的下拉框渲染内容
			$('.whole-country').append(preliminary.selected_el_city_steel);
			$('.Varieties').append(preliminary.selected_el_varieties_steel);
			//datatable 标题渲染相应内容
			$('.datatable_title_wrap').html('').append(preliminary.datatable_title_el_steel);
			preliminary.postData_steel.currentPage = currentP;
			postData = preliminary.postData_steel;

		} else if(preliminary.dataObj.Ob == 2) {
			//家电
			$('.waste-title li').eq(2).addClass('bottom_border_red').siblings('li').removeClass('bottom_border_red');
			$('.waste-search').html('').append(preliminary.search_el_home_applicance);
			$('.whole-country').append(preliminary.selected_el_city_steel);
			$('.household-electrical-appliances').append(preliminary.selected_el_varieties_home_applicance);
			$('.datatable_title_wrap').html('').append(preliminary.datatable_title_el_home_applicance);
			preliminary.postData_jd.currentPage = currentP;
			postData = preliminary.postData_jd;

		} else if(preliminary.dataObj.Ob == 7) {
			//纸
			$('.waste-title li').eq(1).addClass('bottom_border_red').siblings('li').removeClass('bottom_border_red');
			$('.waste-search').html('').append(preliminary.search_el_paper);
			$('.whole-country').append(preliminary.selected_el_city_steel);
			$('.Varieties').append(preliminary.selected_el_varieties_paper);
			$('.datatable_title_wrap').html('').append(preliminary.datatable_title_el_paper);
			preliminary.postData_paper.currentPage = currentP;
			postData = preliminary.postData_paper;
		} else {
			alert('哪里出错了');
		};
		//datatable 加载之前 显示loading 图
		$('.list-title-box').html('').append(preliminary.datatable_loading_data_pic);
		//传参数获取数据
		$.ajax({
			type: "post",
			url: "http://47.93.102.34:8088/cmscm/webshop/showShangCheng",
			async: true,
			data: postData,
			dataType: "json",
			success: function(data) {
				var Content_data = data[0].content;
				if(Content_data.length == 0) {
					return;
				};
				//清空 loading图
				$('.list-title-wrap').html('');
				//循环数据渲染页面
				var list_row_el;
				if(preliminary.dataObj.Ob == 1) {
					for(var i = 0; i < Content_data.length; i++) {
						list_row_el = '<div class="resource_btnList"><div class="waste-data"><div>' +
							(i + 1) + '</div><div>' + Content_data[i].code + '</div><div>' + Content_data[i].factory +
							'</div><div>' + Content_data[i].localCity + '</div><div>' + Content_data[i].type +
							'</div><div>' + Content_data[i].species + '</div><div>' + Content_data[i].sizeDes +
							'</div><div>' + Content_data[i].price + '</div><div>' + Content_data[i].size +
							'</div><div class="choose"><div>选购</div></div></div><div>';
						$('.list-title-wrap').append(list_row_el);
					};
				} else if(preliminary.dataObj.Ob == 2) {
					for(var i = 0; i < Content_data.length; i++) {
						list_row_el = '<div class="resource_btnList1"><div class="waste-data1"><div>' + (i + 1) +
							'</div><div>' + Content_data[i].shopCode + '</div><div>' + Content_data[i].disassembleFactory +
							'</div><div>' + Content_data[i].city + '</div><div>' + Content_data[i].typeO + '</div><div>' +
							Content_data[i].specification + '</div><div>' + Content_data[i].unitPrice + '</div><div>' +
							Content_data[i].state + '</div><div class="choose"><div>选购</div></div></div><div>';
						$('.list-title-wrap').append(list_row_el);
					};
				} else if(preliminary.dataObj.Ob == 7) {
					for(var i = 0; i < Content_data.length; i++) {
						list_row_el = '<div class="resource_btnList"><div class="waste-data"><div>' +
							(i + 1) + '</div><div>' + Content_data[i].code + '</div><div>' + Content_data[i].factory +
							'</div><div>' + Content_data[i].localCity + '</div><div>' + Content_data[i].type +
							'</div><div>' + Content_data[i].species + '</div><div>' + Content_data[i].sizeDes +
							'</div><div>' + Content_data[i].price + '</div><div>' + Content_data[i].size +
							'</div><div class="choose"><div>选购</div></div></div><div>';
						$('.list-title-wrap').append(list_row_el);
					};
				} else {
					alert('哪里出错了')
				};
				preliminary.numpag(data, preliminary.dataObj.currentPage); //根据数据 条数 生成分页按钮
				$('.pageg').html(preliminary.dataObj.allPage); //共多少页
			}
		});

		//如果是家电的 家电规格下拉框 渲染数据
		if(preliminary.dataObj.Ob == 2) {
			preliminary.size_home_applicance("废旧洗衣机");
		};
	},
	//废家电  废钢铁  废纸  tap按钮 
	Clik: function() {
		//废钢铁
		$('.steel-a').click(function() {
			preliminary.dataObj.Ob = 1;
			preliminary.dataObj.currentPage = 1;
			preliminary.postData_steel = {
				type: 1, //种类 1，废钢铁7，废纸默认 1 废钢铁
				localcity: "", //城市
				tradeName: "", //品种
				steelMill: '', //钢厂
				currentPage: 1, //当前页 默认为1
				numPerPage: 20, //总页数判断 每页多少条数据 默认为20
				store: '', //仓库
			};
			//
			preliminary.AJAX();
		});
		//废家电
		$('.Waste-home-appliance').click(function() {
			preliminary.dataObj.Ob = 2;
			preliminary.dataObj.currentPage = 1;
			preliminary.postData_jd = {
				type: 2, //种类 1，废钢铁7，废纸默认 1 废钢铁
				localcity: "", //城市
				specise: "", //品种
				size: '', //规格
				disassembleFactory: '', //拆解厂
				currentPage: 1, //当前页 默认为1
				numPerPage: 20 //总页数判断 每页多少条数据 默认为20
			};
			preliminary.AJAX();
		});
		//废纸
		$('.waste-paper').click(function() {
			preliminary.dataObj.Ob = 7;
			preliminary.dataObj.currentPage = 1;
			preliminary.postData_paper = {
				type: 7, //种类 1，废钢铁7，废纸默认 1 废钢铁
				localcity: "", //城市
				specise: "", //品种
				size: '', //规格
				disassembleFactory: '', //拆解厂
				currentPage: 1, //当前页 默认为1
				numPerPage: 20 //总页数判断 每页多少条数据 默认为20
			};
			preliminary.AJAX();
		});
	},
	//点击下一页  上一页
	nextA: function() {
		//下一页
		$('.paging .nextPage').click(function() {
			preliminary.dataObj.currentPage++;
			preliminary.AJAX();
		});
		//上一页
		$('.paging .prePage').click(function() {
			preliminary.dataObj.currentPage--;
			preliminary.AJAX();
		});
	},
	//跳转页
	Jump_page_number: function() {
		//点击数字按钮
		$(document).on('click', '.num_button', function() {
			preliminary.dataObj.currentPage = $(this).html();
			preliminary.AJAX();
		});
		//点击跳转页
		$(document).on('click', '.goNumPage', function() {
			var numPageInp = $('.numPageInp').val();
			if(numPageInp > preliminary.dataObj.allPage) {
				alert('超出总页数！');
				return;
			} else if(numPageInp < 1) {
				alert('页数不能小于1！');
				return;
			};
			preliminary.dataObj.currentPage = numPageInp;
			preliminary.AJAX();
		});
	}
};

$(function() {
	//初始化页面
	preliminary.init();
	//废家电  废钢铁  废纸  tap按钮 
	preliminary.Clik();
	//点击下一页  上一页
	preliminary.nextA();
	//跳转页
	preliminary.Jump_page_number();
	//找货
	preliminary.clickSearchButton();
	//家电页面品种下拉框选择 获取型号
	preliminary.home_application_select_change();
});